//
//  mainViewController.m
//  PostMessa
//
//  Created by never down on 15-5-25.
//  Copyright (c) 2015年 ygy_ios. All rights reserved.
//

#import "mainViewController.h"
#import "homeViewController.h"
#import "shopViewController.h"
#import "postGoodsViewController.h"
#import "messageViewController.h"
#import "myownViewController.h"
#import "loginViewController.h"
#import "PostMessa-Prefix.pch"

@interface mainViewController (){
    NSArray *btnImage;
    NSArray *btnClickedImage;
    NSArray *btnTitle;  //按钮文字
    UITableView *contentTableview;//中心tableview
    UIView *tabBarIV;   //低端tab条
    
}

@end

@implementation mainViewController
#define NAVMARGINLEFT 15   //左边空隙

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addNotifications];
    [self.view setBackgroundColor:ERHUOBACK];
    btnTitle = [NSArray arrayWithObjects:@"首页",@"求购",@"我要卖",@"消息",@"个人", nil];
    
    self.wantsFullScreenLayout=YES;
    self.tabBar.hidden = YES;
   
    [self initUI];
    
    homeViewController *homeVC = [[homeViewController alloc] init];
    UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:homeVC];
    homeNav.navigationBarHidden = YES;
    
    shopViewController *shopVC = [[shopViewController alloc] init];
    UINavigationController *shopNav = [[UINavigationController alloc] initWithRootViewController:shopVC];
    shopNav.navigationBarHidden = YES;
    
    postGoodsViewController *postVC = [[postGoodsViewController alloc] init];
    UINavigationController *postNav = [[UINavigationController alloc] initWithRootViewController:postVC];
    postNav.navigationBarHidden = YES;
    
    messageViewController *messVC = [[messageViewController alloc] init];//消息
    UINavigationController *messNav = [[UINavigationController alloc] initWithRootViewController:messVC];
    messNav.navigationBarHidden = YES;
    
    myownViewController *myVC = [[myownViewController alloc] init];
    UINavigationController *myNav = [[UINavigationController alloc] initWithRootViewController:myVC];
    myNav.navigationBarHidden = YES;
    
    self.viewControllers = @[homeNav,shopNav,postNav,messNav,myNav];

}
#define BORDERWIDTH 0.5f
#define BOTTOMBARHEIGHT (49 * self.view.bounds.size.height / 568.0f)    //底部导航栏的高度

#define TABNUM 5    //底部导航栏标签个数
#define MARGINTOP 2    //底部导航栏标签顶部留白的宽度
#define MARGINLEFT 4    //底部导航栏标签左右留白的宽度
#define TITLEHEIGHT 6  //底部导航栏文字的大小
#define LINEHEIGHT TITLEHEIGHT * 3
#define BADGEWIDTH 18
#define DOTWIDTH 8
-(void)initUI{
    tabBarIV = [[UIView alloc]initWithFrame:CGRectMake(-BORDERWIDTH, self.view.bounds.size.height - BOTTOMBARHEIGHT, self.view.bounds.size.width + BORDERWIDTH * 2, BOTTOMBARHEIGHT + BORDERWIDTH)];
//    contentTableview = [[UIView alloc]initWithFrame:CGRectMake(-BORDERWIDTH, 0, self.view.bounds.size.width + BORDERWIDTH * 2, self.view.bounds.size.height - BOTTOMBARHEIGHT)];
//    contentTableview.backgroundColor = [UIColor grayColor];
    
    
    tabBarIV.userInteractionEnabled = YES;
    [tabBarIV setBackgroundColor:[UIColor whiteColor]];
    tabBarIV.layer.borderWidth = BORDERWIDTH;
    tabBarIV.layer.borderColor = [ERHUORED(0.5f) CGColor];
    tabBarIV.clipsToBounds = YES;

    float btnWidth = (self.view.frame.size.width - (TABNUM + 1) * MARGINLEFT)/TABNUM;
    float btnHeight = BOTTOMBARHEIGHT - MARGINTOP * 2 - BORDERWIDTH;
    for (NSInteger i = 0; i<btnTitle.count; i++) {
        UIButton *tabBtn = [[UIButton alloc] initWithFrame:CGRectMake(MARGINLEFT * (i + 1) + btnWidth * i + BORDERWIDTH, MARGINTOP + BORDERWIDTH, btnWidth, btnHeight)];
        tabBtn.tag = BUTTONTAG + i;
        UIImage *img = [UIImage imageNamed:btnImage[i]];
        tabBtn.imageEdgeInsets = UIEdgeInsetsMake(MARGINTOP * 1.5, (btnWidth - img.size.width) / 2, btnHeight - img.size.height - MARGINTOP * 1.5, (btnWidth - img.size.width) / 2);
        [tabBtn setImage:img forState:UIControlStateNormal];
        [tabBtn setImage:[UIImage imageNamed:btnClickedImage[i]] forState:UIControlStateSelected];
        [tabBtn setImage:[UIImage imageNamed:btnClickedImage[i]] forState:UIControlStateHighlighted];
        if (i == 2) {
            [tabBtn setBackgroundColor:ERHUORED(1.0f)];
            tabBtn.layer.cornerRadius = 3;
            [tabBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [tabBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        }else {
            [tabBtn setTitleColor:ERHUOTEXT forState:UIControlStateNormal];
            [tabBtn setTitleColor:ERHUORED(1.0f) forState:UIControlStateSelected];
        }
        [tabBtn setTitle:btnTitle[i] forState:UIControlStateNormal];
        tabBtn.titleLabel.font = [UIFont systemFontOfSize:11];
        tabBtn.titleEdgeInsets = UIEdgeInsetsMake(btnHeight - LINEHEIGHT, -img.size.width, 0, 0);
        [tabBtn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
        [tabBarIV addSubview:tabBtn];
    }
    [self.view addSubview:tabBarIV];
//    [self.view addSubview:contentTableview];
    
    ((UIButton *)[self.view viewWithTag:BUTTONTAG]).selected = YES;
    self.selectedIndex = 0;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation
-(void)btnTapped:(UIButton *)sender{
    [self.view bringSubviewToFront:tabBarIV];
    if (self.view.frame.origin.x > 0) {
        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        }completion:nil];
    }
    
    __block NSUInteger newselected = sender.tag - BUTTONTAG;
    if (newselected == self.selectedIndex) {
        [((UINavigationController *)self.selectedViewController) popToRootViewControllerAnimated:YES];
//        self.selectedViewController.parentViewController;
    }
    
    if (newselected > 1) {
        NSInteger uid  = [[[NSUserDefaults standardUserDefaults] objectForKey:@"user"] objectForKey:@"uid"];
        if (uid < 1) {
            [self.view sendSubviewToBack:tabBarIV];
            loginViewController *loginVC = [[loginViewController alloc] init];
            [self presentViewController:loginVC animated:NO completion:nil];
            //[self.tabBarController.navigationController pushViewController:loginVC animated:NO];

            
            CATransition* transition = [CATransition animation];
            transition.type = kCATransitionPush;
            transition.subtype = kCATransitionFromRight;
            transition.duration = 0.2f;
            [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
        }
    }
    
}

- (void)backToHome:(NSNotification *)notification {
    NSLog(@"123");
    if (!notification) {
        return;
    }
    NSInteger index = [notification.object integerValue];
    if (index >= 0) {
        [self btnTapped:(UIButton *)[tabBarIV viewWithTag:BUTTONTAG + index]];
    }
    
    if (self.selectedIndex != 2) {
        [self.view bringSubviewToFront:tabBarIV];
    }
}



- (void)addNotifications{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backToHome:) name:@"backToHome" object:nil];
}
@end
